package cn.wx.system.controller;


import cn.wx.basic.util.JsonResult;
import cn.wx.basic.util.PageList;
import cn.wx.system.domain.Permission;
import cn.wx.system.query.PermissionQuery;
import cn.wx.system.service.IPermissionService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/permission")
@Api(value = "管理权限的API",description="管理权限的CRUD功能")
public class PermissionController {

    @Autowired
    private IPermissionService iPermissionService;

    //使用get方式请求  查询所有
    @GetMapping
    @ApiOperation(value = "查询所有" )
    public List<Permission> findAll(){
        return iPermissionService.loadAll();
    }

    @GetMapping("/{id}")   //获取get请求里面的参数   查询一个
    @ApiOperation(value = "通过ID查询" )
    public  Permission findById(@PathVariable("id") Long id ){
        return iPermissionService.loadById(id);
    }

    //删除
    @DeleteMapping("/{id}")
    @ApiOperation(value = "通过ID删除" )
    public JsonResult delete(@PathVariable("id") Long id){
        try {
            iPermissionService.remove(id);
            return JsonResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return JsonResult.me().setMsg("操作失败");
        }
    }

    //添加和更新     //使用put方式请求
    @PutMapping
    @ApiOperation(value = "增加修改" )
    public JsonResult save(@RequestBody  Permission  permission){
        try {
            if( permission.getId()==null){
                iPermissionService.save( permission);
            }else{
                iPermissionService.update( permission);
            }
            return JsonResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return JsonResult.me().setMsg("操作失败");
        }
    }

    //分页
    @PostMapping      //使用post方式请求
    @ApiOperation(value = "分页查询" )
    public PageList< Permission> queryPage(@RequestBody PermissionQuery query){

        try {
            PageList<Permission> permissionPageList = iPermissionService.pageQuery(query);
            return  permissionPageList;
        } catch (Exception e) {
            e.printStackTrace();
            return new PageList<>();
        }
    }

    //批量删除
    @PatchMapping
    public JsonResult  patchDel(@RequestBody List<Long> ids){
        try {
            iPermissionService.patchDel(ids);
            return JsonResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return JsonResult.me().setMsg("操作失败");
        }

    }


}
